<?php
namespace Home\Controller;
use Think\Controller;
use Think\Think;

class IndexController extends Controller {
    public function index()
    {
         //先查询分类
        $cate=M('Cate')->select();

        foreach ($cate as $key=>$value){
          //根据分类中的id查询商品表中的商品，前四条
            $list=M('Goods')->where('c_id='.$value['id'])->limit(4)->select();
           //判断对应的分类下是否有商品
            if($list !=''){
                //如果有保存起来
                $cate[$key]['goods']=$list;
            }
            //再次查询后四条
            $two=M('Goods')->where('c_id='.$value['id'])->limit(4,8)->select();
            if($two !=''){
                $cate[$key]['two']=$two;
            }
        }

        $this->assign('cate',$cate);

        $this->display();
    }
    public function proDetails(){
        $info=$_GET;
        $data=M('Goods')->find($_GET['id']);
       $this->assign('cate',$_GET['cname']);
       $this->assign('data',$data);
        $this->display();
    }
    //更多
    public  function more(){
        $id=I('get.id');
        $cate=M('Cate')->find($id);
        $data=M('Goods')->where("c_id=".$id)->select();
        $this->assign('cate',$cate['cname']);
        $this->assign('data',$data);
        $this->display();
    }
    public function captcha(){
        //配置属性
        $cfg=array(
            'length'    =>  4,         // 验证码位数
            'useCurve'  =>  false,        // 是否画混淆曲线
            'fontttf'   =>  '4.ttf',       // 验证码字体，不设置随机获取
        );
        $verify=new \Think\Verify($cfg);//实例化验证码对象，并传递参数
        //输出验证码
        $verify->entry();
    }
    public function order(){
        if(IS_POST){
            $data=I('post.');
            //补全字段
            $data['xd_time']=time();
            //添加到数据库
            $result=M('Order')->add($data);
            if($result){
                $this->success('订单提交成功，请耐心等待，我们会尽快为你发货！',U('index'),1);
            }else{
                $this->error('订单提交失败！');
            }
        }else{
            $data=$_GET;
            $zj=$_GET['gwzj']*$_GET['number'];
            $this->assign('sum',$zj);
            $this->assign('data',$data);
            $this->display();
        }

    }
    public function myOrder(){
        $id=I('get.id');
        $data=M('Order')->where("uid=$id")->select();
        $this->assign('data',$data);
        $this->display();

    }
    public function changePwd(){
        $where['username']=I('get.uname');
        if(IS_POST){
            $model=M('User');
            $data=I('post.');
            $where['password']=md5($data['username1']);

            $pd=$model->where($where)->find();
            $id=$pd['id'];
            if($pd){
                if($data['username2']==$data['username']){
                    $pwd=md5($data['username']);
                    $result=$model->save(array('id'=>$id,'password'=>$pwd));
                    if($result){
                        session(null);
                        $this->success('密码修改成功，请重新登录！',U('User/login'),1);
                    }else{
                        $this->error('密码修改失败');
                    }

                }else{
                    $this->error('两次密码输入不一致，请重新输入!');
                }

            }else{
                $this->error('原始密码错误，请重新输入！');
            }
        }else{
            $this->display();
        }


    }
    public function reg(){
        $model=M('User');
        if(IS_POST){
            $post=I('post.');
            //加密密码
            $post['password']=md5($post['password']);

            $verify=new \Think\Verify();
            $result=$verify->check($post['captcha']);
            //删除验证码元素
            unset($post['captcha']);
            if($result){
                $rules=array(
                    array('username','require','用户名必须!'),
                    array('username','','用户名已经存在!',0,'unique',1),
                    array('password','require','密码必须!'),
                    array('sex',array('男','女'),'性别只能是男或女!',3,'in'),
                    array('email','email','邮箱格式错误！')
                );
                //判断验证是否通过
               if($model->validate($rules)->create()){
                          $post['regtime']=time();//补全字段
                       $result=$model->add($post);
                       if($result){
                           $this->success('注册成功，赶快去登录吧！',U('User/login'),1);
                       }else{
                           $this->error('注册失败！');
                       }
               }else{
                   $this->error($model->getError());
               }
            }else{
                $this->error('输入的验证码有误！');
            }
        }else{
            $this->display();
        }

    }

}
